<?php

namespace App\Repositories;

use App\Models\Category;

use App\Models\Product;

use App\Exceptions\InvalidException;

class CategoryRepo
{
    public function getCategoryName(Product $product)
    {
        if(!$product) {
            throw new InvalidException("产品不存在");
        }
        return Category::where('id','in',$product->cate_id)->get();
    }
    public function getInfo($cate_id)
    {
        return Category::where('id',$cate_id)->first();
    }

    public function allCate()
    {

        $category =  Category::select('id','name','pid','url')->get()->toArray();

        if(!$category) {
            return [];
        }


        $catelist = self::buildCategory($category);
        return $catelist;
    }

    public static function getProductsByCateId($cate_id,$products)
    {
        $products = [];
        foreach($products as $item) {
            if($item['cate_id'] == $cate_id)
            {
                $products[] = $item;
            }
        }
        return $products;
    }
    public function addCate($data)
    {
        try{
            Category::create($data);
        }catch(\Exception $e) {
            throw new InvalidException($e->getMessage());
        }
    }

    public function updateCate($data)
    {
        $category  = Category::where('id',$data['id'])->first();
        if(!$category) {
            throw new InvalidException('分类不存在');
        }
        try{
            $category->name = $data['name'];
            $category->pid = $data['pid'];
            if(!empty($data['url'])) {
               $category->image = $data['url']; 
            }
            $category->save();
        } catch(\Exception $e){
            throw new InvalidException($e->getMessage());
        }
    }
    public function getCategoryList($pid = 0)
    {

        $category =  Category::select('id','name','pid','url')->with(['products'=>function($query){
            $query->where('status',1)->select('id','cate_id','name','image','price');
        }])->get()->toArray();
        if(!$category) {
            return [];
        }
        return self::buildCategory($category);
    }

    public function buildCategoryProducts(array $category,array $products)
    {

        $category_list = [];
        $second = [];


        foreach($category as $cate) {
            if($cate['pid'] == 0) {
                $category_list[] = $cate;
            } else {
                $second[] = $cate;
            }
        }


        foreach($category_list as &$fir)
        {
            foreach($second as $sec) {

                if($sec['pid'] == $fir['scate_id']) {
                    //获取当前分类得产品
                    $sec['products'] =  self::getProductsByCateId($sec['id'],$products);

                    $fir['children'][] = $sec;
                }
            }
        }

        return $category_list;
    }

    public static function buildCategory(array $category)
    {
        $category_list = [];
        $second = [];


        foreach($category as $cate) {
            if($cate['pid'] == 0) {
                $category_list[] = $cate;
            } else {
                $second[] = $cate;
            }
        }


        foreach($category_list as &$fir)
        {
            foreach($second as $sec) {
                if($sec['pid'] == $fir['id']) {
                    $fir['children'][] = $sec;
                }
            }
        }

        return $category_list;
    }



    //==============================================API接口===============================
    //获取分类
    public function getLevelSecList($where,$page=15)
    {
        return Category::where(function($query){
            $query->where('level',2);
        })->with([
            'products'
        ])->get();
        return Category::where(function($query)use($where){

        })->with([
            'products'=>function($query){

            },
        ])->paginate(15);

    }

    public function getProducts($where,$page=15)
    {
        return Product::where(function($query) use ($where){
            if(!empty($where['cate_id'])) {
                $query->whereRaw('FIND_IN_SET(?,cate_id)',$where['cate_id']);
            }
        })->paginate($page)->toArray();
    }

    public function getSimpleCategory()
    {
        return Category::select(['id','name'])->get();
    }
}
